package Easy比特位数;

import org.testng.annotations.Test;

import java.util.Locale;

public class Solution {
    public int[] countBits(int n) {
        int[] byt = new int[n+1];
        int num;
        for (int i = 1; i < n+1; i++) {
            num = i;
            while(num!=0){
                if (num%2!=0)
                    byt[i]++;
                num = num/2;
            }
        }
        return byt;
    }
    public int[] countBits1(int n) {
        int[] byt = new int[n+1];
        if (n==0)
            return byt;
        byt[1] = 1;
        int num=0;
        int num1=0;
        for (int i = 2; i < n+1; i++) {
            if ((i&-i) == i){
                byt[i] = 1;
                num = i;
                num1 = 1;
            }
            else{
                byt[i] = byt[num]+byt[num1];
                num1++;
            }
        }
        return byt;
    }
    public int[] countBits2(int n) {
        int[] byt = new int[n+1];
        for (int i = 0; i < n+1; i++) {
            byt[i] = Integer.bitCount(i);
        }
        return byt;
    }
    @Test
    public void test(){
        int[] num = countBits2(15);
        for (int i:
                num) {
            System.out.println(i);
        }

    }
}
